package controllers;

import play.*;
import play.mvc.*;
import util.FsUtil;

import java.io.ByteArrayInputStream;
import java.util.*;

import com.google.appengine.api.blobstore.BlobInfo;
import com.google.appengine.api.blobstore.BlobInfoFactory;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.images.Image;
import com.google.appengine.api.images.ImagesService;
import com.google.appengine.api.images.ImagesServiceFactory;

import models.*;

public class Application extends Controller {

	//show home page
    public static void index() {
    	List<CategoryModel> dongphucCategories = CategoryModel.listDongPhuc();
    	List<CategoryModel> bhldCategories = CategoryModel.listBHLD();
        render(dongphucCategories, bhldCategories);
    }
    //show picture
    public static void showPicture(String path, int height, int width){
		BlobKey key = new BlobKey(path);
		BlobInfoFactory factory = new BlobInfoFactory();
		BlobInfo info = factory.loadBlobInfo(key);
		ImagesService imagesService = ImagesServiceFactory.getImagesService();
        Image oldImage = ImagesServiceFactory.makeImageFromBlob(key);
        response.cacheFor("48h");
        response.setContentTypeIfNotSet(info.getContentType());
		renderBinary(new ByteArrayInputStream(FsUtil.scaleImage(imagesService, oldImage, height, width)));
	}

}